<?php

namespace App\Models;

use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/*总数字资源类型model*/

class ResourceTypeYrd extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    protected $table = 'resource_type_yrd';

    /**
     * 获取类型名称，没有则添加资源
     **/
    public function getTypeNameNoAdd($data)
    {
        $type = $this->where('original_id', $data['id'])->where('is_del', 1)->first();
        if ($type) {
            $type->original_id = $data['id'];
            $type->level = $data['treeGrade'];
            $type->type_name = $data['name'];
            $type->save();
            return $type->id;
        } else {
            $obj = new self();
            $obj->original_id = $data['id'];
            $obj->level = $data['treeGrade'];
            $obj->type_name = $data['name'];
            $obj->save();
            return $obj->id;
        }
    }
}
